home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / C++ Direct Buffer Access Code / Blitting.sit / Blitting Folder ƒ / PUT IN--“Metrowerks C_C++” ƒ / Personal Library / Blitting Class Library / BCL Manual next >
Text File  |  1995-08-04  |  8KB  |  93 lines

  1. The Blitting Class Library Manual
  2. Copyright ⌐ 1995, Macneil Shonle. All rights reserved.
  3.  
  4. Contents
  5.  Introduction
  6.  Use of BufferAccessor
  7.  About the Sprite class
  8.  What is to come
  9.  Frequently Asked Questions
  10.  Notes on Style
  11.  In Closing
  12.  
  13. Introduction
  14. The following component explained here is the evolution of some direct blitting classes I had made for my own personal use. As I used the classes more and more I was able to see what was needed in the classes, and what wasn╒t. The result was a much simpler and cleaner design that can be useful to anyone interested in speeding up their graphics routines.
  15.  
  16. If you want to directly access a GWorld, all you have to do is supply a pointer to it in the constructor for a GWorldAccessor object. Take a look at the following code, it will directly access the GWorld╒s pixel-map to draw a black dot at coordinate (12, 16), that is, assuming that myGWorld is pointing to a GWorld created with NewGWorld:
  17.  
  18.     GWorldAccessor gworldBuff(myGWorld);
  19.     gworldBuff.setPixel(12, 16, 0xFF);
  20.  
  21. Previously, the same class that provided access to the pixel-map also created it. This looked simpler on the outside, but was too limiting to users of other frameworks and put too much complexity into one class. For users who liked the encapsulation of the GWorld (so they didn╒t have to worry about calling NewGWorld and DisposeGWorld), I have provided a set of SecureMacPorts to be used:
  22.  
  23.     Rect bounds = {0, 0, 400, 400};
  24.     SecureGWorld gworld(bounds);
  25.     
  26.     GWorldAccessor gworldBuff(gworld);
  27.     // ...
  28.  
  29. Use of BufferAccessor
  30. The power of polymorphism can be shown by having a series of blitting functions use a BufferAccessor& as their parameter. For example, look at this (inefficient) function that fills the parameter╒s pixel-map to black:
  31.  
  32.     void Flood(BufferAccessor& buff)
  33.     {
  34.         for (PixelCord y=0; y<buff.getHeight(); y++)
  35.             for (PixelCord x=0; x<buff.getWidth(); x++)
  36.                 buff.setPixel(x, y, 0xFF);
  37.     }
  38.  
  39. Note that this may be a window or a GWorld, the code doesn╒t care and works either way. Now take a look at a more efficient version:
  40.  
  41.     void Flood(BufferAccessor& buff)
  42.     {
  43.         MMUModeSwapper swapTo(buff.use32Bit());
  44.     
  45.         FourPixelsPtr dest = buff.getBaseAddr();
  46.         RowBytes rowSkip = buff.getRowBytes() - buff.getWidth();
  47.         PixelCord height = buff.getHeight()
  48.         PixelCord width = buff.getWidth() / 4;
  49.     
  50.         for (PixelCord y=0; y<height; y++) {
  51.             for (PixelCord x=0; x<width; x++)
  52.                 *dest++ = 0xFFFFFFFF;
  53.             dest = FourPixelsPtr(PixelPtr(dest) + rowSkip);
  54.         }
  55.     }
  56.  
  57. This can be optimized even more, but the use and simplicity of the accessors should be clear now. You may have noticed the use of the MMUModeSwapper class (from <MMUModeSwapper.h>). It uses the ╥resource acquisition is initialization╙ technique (see 9.4 of Stroustrup). When the object is initialized, the constructor puts the machine into the correct addressing mode. It is up to the destructor to return the machine back to the previous state. Similar to the MMUModeSwapper, there is a GWorldSetter. This technique is useful because (1) it reduces the amount of code to be writen, in a clear and elegant manner; (2) it ensures that the state will always be returned, important for when an exception is thrown; and (3) a programmer might forget to return the state back otherwise.
  58.  
  59. The header <PixelTypes.h> is useful if ever the bytes-per-integral-type change in an implementation; that way, all of the code that will have to be rewriten would be in one place (the header).
  60.  
  61. About the Sprite class
  62. This release comes with a CIconSprite class, which has not been heavily tested but appears to work for my purposes. Not all of the member-functions for it are implemented, such as member-functions providing persistence. The sample program MySprite uses them, and another class called ForeGroundObject.
  63.  
  64. (The sample program isn╒t even event driven. Click the mouse to quit it, or wait until profiling is done. Always use the profiler. Metrowerk╒s has the best Macintosh profiler I╒ve seen.)
  65.  
  66. Please tell me what you would like to see in the sprite classes, as they are incomplete. Note that the sprite abstraction is only for drawing the images and handling the bitmaps, nothing else. More complex code that handles posistions and animation frames belong in something like ForeGroundObject, not in the raw drawing classes for them.
  67.  
  68. What is to come
  69. In the ╥Blitting Class Library╙ folder, there is a folder named ╥Plans,╙ which includes a sample of what the ImageCopier class will look like. This should be useful for programs that can change their mind at run time about which copy method to use (CopyBits vs. direct). In an imformal test I did on the PowerPC, the CopyBits routine took 80% of all processor time (mind you it was calling CopyBits a lot). I replaced all calls to CopyBits with a blitter of my own, and total processor time dropped to 60%.
  70.  
  71. Collision detection will be added into the sprite classes. A class for fading the screen is also planned, and I╒ll probably throw in some functions for hiding the menu bar, just for sport.
  72.  
  73. Frequently Asked Questions
  74. Ñ What is Blitting?
  75. The word ╥blit╙ came from the PDP-11 instruction BLT (BLock Transfer). This instruction was used to move graphics from one place to the other, back in the old days. Now blit has a more general meaning and implies graphics operations. For example, something that draws sprites on the screen would be a sprite blitter.
  76.  
  77. Ñ What is a Sprite?
  78. A sprite is any moving object on the screen that has a mask.
  79.  
  80. Ñ Why can╒t the code compile? Something is wrong with the headers, I think.
  81. Move the ╥Blitting Class Library╙ folder to the system level (i.e. the folder with the compiler in it). That╒s because I use #include <name.h> instead of #include "name.h". That╒s all.
  82.  
  83. Notes on Style
  84. The style of the code has changed completly since the CDirectBlit release of these classes (you can call this  the BufferAccessor release). Support for exceptions have been brought in, and everything is much more atomic (the two changes went hand-in-hand). It was much harder to design this version than past versions, but it was much simpler to implement.
  85.  
  86. On a less important note: The naming coventions have been changed, and no longer use PowerPlant-like prefixes. The name CDirectBlit was a misnomer if I ever saw one, and was briefly changed to DirectBuffer before the design changed to the current accessor version. I also changed the member-function naming convention so that the first letter of each function is in lower case, which matches the good style I╒ve seen in the books: Advanced C++ Programming Styles and Idioms and C++ FAQs, and in Smalltalk. Another note: mangling the data member names seems kind of silly, if you ask me. The brace placement has been changed to the more traditional K&R style. I still use the ╥#ifndef/#define/#endif╙ technique to avoid the inclusion of a header file twice, which is less implementation dependant than ╥#pragma once╙ is.
  87.  
  88. In Closing
  89. These classes have been made to aid blitting code for those who do not want to change their project to a whole new library when all they want to do is just get the base address of a GWorld. These are the people I had in mind while writing this code. If you have a GWorld already made with PowerPlant, you can still benefit with direct access, and with my classes.
  90.  
  91. Finally: I need more input from people who have downloaded these classes. Do you like them? Do you hate them? Is there something you wished they had or did or didn╒t do? Any input will help me. Send all comments via email to: macneils@aol.com.
  92.  
  93. Feel free to upload this code anywhere, but only the original archive. You may change the code for your own uses (such as making functions inline), but you are not given permission to distribute it in the changed form. Use the Blitting Class Library, or parts of it, at your own risk. If the libarary is used in a project, you are not obligated to give credit╔ but it would be nice if you were to. The code is free (donations accepted), enjoy!